<?php
namespace app\api\controller\newfile;

use think\Request;
use app\common\model\newmodel\User as UserModel;
use app\common\model\newmodel\UserAgency;
use app\common\model\newmodel\UserFactory;
use app\common\model\power\Rolepower;
use app\api\controller\Pub;
use app\api\controller\Base;

class User extends Base
{
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(301001);
	}

    /**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}

    /**
     * 获取员工列表（根据当前用户角色自动判断是工厂还是代理商）
     */
    public function userList(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 10);
        $keyword = $request->get('keyword', '');
        $userId = Pub::userid();
        // 获取当前用户信息
        $userInfo = UserModel::get($userId);
        $role = $userInfo->Role;

        if ($role==301) {
            // 工厂用户 - 获取工厂员工列表
            $result = UserModel::getFactoryStaffList($userId, $page, $pageSize, $keyword);
        } elseif ($role == 401) {
            // 代理商用户 - 获取代理商员工列表
            $result = UserModel::getAgencyStaffList($userId, $page, $pageSize, $keyword);
        } else {
            return json(['code' => 0, 'msg' => '无权限访问']);
        }

        return json($result);
    }

    //添加员工
    public function userAdd()
    {
        $data = input('post.');
        if (empty($data['Iphone']) || empty($data['Name']) || empty($data['sex'])) {
            return json(['code' => 0, 'msg' => '参数不完整']);
        }
        
        $userId = Pub::userid();
        $userInfo = UserModel::get($userId);
        $role = $userInfo->Role;
        
        if ($role == 301) {
            // 工厂管理员 - 添加工厂员工
            $factoryId = UserFactory::getFactoryIdByUserId($userId);
            if (!$factoryId) {
                return json(['code' => 0, 'msg' => '未绑定工厂']);
            }
            $result = UserModel::addFactoryStaff($factoryId, $data);
        } elseif ($role == 401) {
            // 代理商管理员 - 添加代理商员工
            $agencyId = UserAgency::getAgencyIdByUserId($userId);
            if (!$agencyId) {
                return json(['code' => 0, 'msg' => '未绑定代理商']);
            }
            $result = UserModel::addAgencyStaff($agencyId, $data);
        } else {
            return json(['code' => 0, 'msg' => '无权限添加员工']);
        }
        
        return json($result);
    }

    //员工编辑
    public function userEdit()
    {
        $data = input('post.');
        if (empty($data['UserId']) || empty($data['Name']) || empty($data['Iphone']) || empty($data['sex'])) {
            return json(['code' => 0, 'msg' => '参数不完整']);
        }
        $result = UserModel::editStaff($data);
        return json($result);
    }

    //员工删除
    public function userDel(Request $request)
    {
        $user_id=Pub::userid();
        $user=UserModel::get($user_id);
        $role=$user->Role;
        $userId =$request->post('UserId');
        if (empty($userId)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        UserModel::where('UserId',$userId)->update(['State'=>3]);
        if($role==301){
            UserFactory::delUserFactory($userId);
        }elseif($role==401){
            UserAgency::delUserAgency($userId);
        }
        return json(['code'=>1, 'msg'=>'删除成功']);
    }
}
